package m.jclecai.com.activity;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.squareup.okhttp.Request;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import m.jclecai.com.R;
import m.jclecai.com.adapter.OrderDetailAdapter;
import m.jclecai.com.base.BaseActivity;
import m.jclecai.com.bean.OrderDetailBean;
import m.jclecai.com.bean.RecordsBean;
import m.jclecai.com.constant.Constant_Url;
import m.jclecai.com.utils.http.FastOk;
import m.jclecai.com.utils.http.HttpManager;

/**
 * Created by zd on 2017/9/29 0029.
 */

public class OrderDetailActivity extends BaseActivity implements PullToRefreshListView.OnRefreshListener2 {

    @BindView(R.id.pr_listview)
    PullToRefreshListView mPrListview;
    OrderDetailAdapter adapter;
    int page = 1;
    String id;
    List<RecordsBean> records;

    @Override
    protected int getContentView() {
        return R.layout.activity_order_detail;
    }

    @Override
    protected void initView() {
        super.initView();
        setTitle("出票明细");
        leftOptionText("");
        records = new ArrayList<>();
        adapter = new OrderDetailAdapter(this, records);
        mPrListview.setAdapter(adapter);
        mPrListview.setOnRefreshListener(this);
        showLoadingLayout(mPrListview);
    }

    @Override
    protected void initData() {
        super.initData();
        id = getIntent().getStringExtra("id");
        getData();
    }

    void getData() {
        HttpManager.getAsyn(Constant_Url.TICKETLIST + "?id=" + id + "&page=" + page, new HttpManager.StringCallback() {
            @Override
            public void onFailure(Request request, IOException e) {
                showExceptionLayout();
            }

            @Override
            public void onResponse(String response) {
                mPrListview.onRefreshComplete();
                closeLoadingLayout();
                OrderDetailBean bean = FastOk.get(response, OrderDetailBean.class);
                if (bean != null && bean.getRecords() != null) {
                    if (bean.getRecords().size() > 0)
                        page++;
                    records.addAll(bean.getRecords());
                }
                if (records.size() <= 0)
                    showEmptyLayout();
            }
        });
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        page = 1;
        records.clear();
        getData();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {
        getData();
    }

    @Override
    public void OnRetry(String btnMessage) {
        super.OnRetry(btnMessage);
        showLoadingLayout(mPrListview);
        getData();
    }
}
